<?php
namespace soa\parter\zt;
use artisan\http;

/**
 * 中通录入订单类
 * Class EntryOrder
 * @package soa\parter\zt
 */
class Order{
    /**
     * 服务地址
     */
//    protected $service_url = 'http://58.40.16.125:9001/zto/api_utf8/commonOrder';   //测试
    protected $service_url = 'http://japi.zto.cn/zto/api_utf8/commonOrder';     //生产环境

    /**
     * 公司名称
     */
    protected $company_id = 'Kuaibao';

    /**
     * 传输校验的key值
     */
    protected $key = 'fde099cd2cf60738d765c48afb252a58';

    /**
     * 接口类型
     */
    protected $msg_type = '';


    /**
     * 初始化
     * EntryOrder constructor.
     */
    public final function __construct(array $param)
    {
        if(empty($param) || !is_array($param)){
            return null;
        }
        $this->msg_type = $param['msg_type'];
        return $this;
    }


    /**
     * 获取服务名称
     */
    public function getCompanyId()
    {
        return $this->company_id;
    }

    /**
     * 获取服务地址
     */
    public function getServiceUrl()
    {
        return $this->service_url;
    }

    /**
     * 获取校验key
     */
    public function getKey()
    {
        return $this->key;
    }

    /**
     * 接口类型
     */
    public function msg_type()
    {
        return $this->msg_type;
    }


    /**
     * 发送请求
     * @param $data
     * @return mixed
     */
    public function requestApi($data)
    {
        $data = mb_convert_encoding(json_encode($data), 'GBK');             //转GBK
        $data_digest =(base64_encode(md5($data . $this->getKey(), true)));  //签名       签名方式 base64(md5(data字段+key))
        return http::post($this->getServiceUrl(),array(
            'data_digest'=>$data_digest,            //签名
            'company_id'=>$this->getCompanyId(),    //公司名
            'msg_type'=>$this->msg_type(),          //接口类型
            'data'=>$data                           //数据
        ));
    }

    /**
     * 中通录单接口：业务员下单
     * @param $data
     * @return mixed
     */
    public function createDataOfEntryOrder($data)
    {
        $param = array(
            'partnerCode'=>$data['waybillArr'],   //订单号
            'mailNo'=>$data['waybillArr'],         //运单号
            'sender'=>array(
                'name'=>$data['name'],              //发件人姓名
                'mobile'=>$data['sendPhone'],      //发件人手机号码
                'prov'=>$data['sendProvince'],     //发件人省
                'city'=>$data['sendCity'],         //发件人市
                'county'=>$data['sendDistrict'],  //发件人区
                'address'=>$data['sendAddress'],  //发件人详细地址
            )
        );
        return $this->requestApi($param);
    }


    /**
     * 拒单（打回）
     * @param $orders
     * @return mixed
     */
    public function refuseOrderOfZt($orders)
    {
        $data = array();
        foreach ($orders as $order){    //目前orderNo系统订单号 作为保留字段
            if(empty($order['orderNo']) || empty($order['refuseMsg']) || empty($order['thirdOrderNo'])){
                raise(1009, '订单信息有误', '');
            }
            $data[] = array(
                'orderCode'  => $order['thirdOrderNo'], //第三方单号
                'fieldName'  => 'status',
                'fieldValue' => 'cancel',
                'reason'     => $order['refuseMsg'],    //拒单原因
            );
        }
        if(empty($data)){
            return false;
        }
        return $this->requestApi($data);
    }

    /**
     * 回填单号
     * @param $param
     * @return mixed
     */
    public function rewriteWaybillNo($param)
    {
        if(empty($param)){
            return false;
        }
        $data = array(
            'orderCode' => $param['orderNo'],       //订单号
            'billCode'  => $param['waybillNo'],     //运单号
            'manCode'   => $param['manCode'],       //操作员工编号
            'siteCode'  => $param['siteCode'],      //操作网点编号
        );
        return $this->requestApi($data);
    }





}